package zhuyun.code.launcheractivity.view.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.provider.Contacts
import android.widget.ImageView
import androidx.core.graphics.drawable.toBitmap
import androidx.recyclerview.widget.ItemTouchUIUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.utils.GlideUtil
import zhuyun.code.launcheractivity.utils.UIUitls

/**
 * @author: Administrator
 * @date: 2021/12/8
 */
class CircleOnePicAdapter(val context: Context,val data:MutableList<String>):
    BaseQuickAdapter<String,BaseViewHolder>(R.layout.item_circle_one_pic,data) {
    override fun convert(helper: BaseViewHolder, item: String) {
        val imageView = helper.getView<ImageView>(R.id.iv)
        if(itemCount==1){
            Glide.with(context).load(item).apply(RequestOptions())
                .into(object :SimpleTarget<Drawable>(){
                    override fun onResourceReady(
                        resource: Drawable,
                        transition: Transition<in Drawable>?
                    ) {
                        val bitmap = resource.toBitmap()
                        val width = bitmap.width
                        val height = bitmap.height
                        if(width < height){
                            val ratio = width/height.toFloat()
                            val layoutParams = imageView.layoutParams
                            layoutParams.height = UIUitls.dp2px(145.0f)
                            layoutParams.width = UIUitls.dp2px(145.0f * ratio)
                            imageView.layoutParams = layoutParams
                            imageView.setImageBitmap(bitmap)
                        } else {
                            val ratio = height/width.toFloat()
                            val layoutParams = imageView.layoutParams
                            layoutParams.width = UIUitls.dp2px(210.0f)
                            layoutParams.height = UIUitls.dp2px(210.0f * ratio)
                            imageView.layoutParams = layoutParams
                            imageView.setImageBitmap(bitmap)

                        }
                    }

                })
        } else {
            GlideUtil.displayEspImage(item,imageView,R.mipmap.icon_default_200x200)
        }
    }
}